/* 
 * File:   controlador.cpp
 * Author: fer
 * 
 * Created on 26 de agosto de 2011, 0:03
 */
/*This file is part of Sandbox.

Sandbox is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License.

Sandbox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Sandbox.  If not, see <http://www.gnu.org/licenses/>.

For contact, see the About page of my blog: <http://mygeekyware.wordpress.com/>.*/

#include <iostream>
#include "controlador.h"
#include "definiciones.h"
#include <SDL/SDL.h>
#include <SDL/SDL_events.h>
#include "particula.h"
#include <math.h>

using namespace std;

controlador::controlador() {
    brsize = 1;
    brtype = CLEAR;
    brtype2 = CLEAR;
    mouse1pushed = 0;
    reset = false;
    particlescount = 0;
    contadorgeneral = 0;
    speed = 1;
}

controlador::controlador(const controlador& orig) {
}

controlador::~controlador() {
}

void controlador::Update(particula matriz[400][220]) {
    int k;
    if (reset) {
        reset = false;
        int i,j;
        for (i=1;i<399;i++) {
            for (j=1;j<219;j++) {
                Clear(matriz,i,j);
            }
        }
    }
    if (SDL_GetMouseState(&mx,&my) & SDL_BUTTON(1)) {
        if (my > 440) {
            if (mx >800-88) {
                MenuDerecho(mx,my);
            }
            else
                MenuIzquierdo(mx,my,0);
        }
        else
            Pincel(matriz, mx,my);
        mouse1pushed = 1;
    }
    else
        mouse1pushed = 0;

    if (SDL_GetMouseState(&mx,&my) & SDL_BUTTON(3)) {
        MenuIzquierdo(mx,my,1);
    }
    for (k=0;k<speed;k++) {
        UpdateMatrix(matriz);
    }
}

void controlador::Pincel(particula matriz[400][220], int x, int y) {
    int dx,dy,i;
    if (brsize==1) {
        if (matriz[x/2][y/2].tipo == 0)
            SetPixel(matriz, x, y);
    }
    else {
        for (dx = -brsize+1; dx < brsize-1; dx++) {
            i = sqrt(brsize*brsize - dx*dx);
            for (dy=-i+1;dy<i-1;dy++) {
                if (x+dx >= 0 && x+dx < 800 && y+dy >= 0 && y+dy < 440)
                    SetPixel(matriz, x+dx,y+dy);
            }
        }
    }
}

void controlador::SetPixel(particula matriz[400][220], int x, int y) {
    if (matriz[x/2][y/2].tipo != 0 && brtype != CLEAR)
        return;
    int mx = x/2;
    int my = y/2;
    matriz[mx][my].tipo = brtype;
    matriz[mx][my].tipo2 = brtype2;
    switch (brtype) {
        case CLEAR:
            Clear(matriz,mx,my);
            break;
        case WALL:
            matriz[mx][my].peso = 1000;
            break;
        case WATER:
            matriz[mx][my].peso = 2;
            break;
        case SALT:
            matriz[mx][my].peso = 4;
            break;
        case SAND:
            matriz[mx][my].peso = 4;
            break;
        case STONE:
            matriz[mx][my].peso = 4;
            break;
        case SALTWATER:
            matriz[mx][my].peso = 3;
            break;
        case FIRE:
            matriz[mx][my].peso = 0;
            matriz[mx][my].contador = 30;
            break;
        case EARTH:
            matriz[mx][my].peso = 4;
            break;
        case STEAM:
            matriz[mx][my].peso = 0;
            break;
        case ICE:
            matriz[mx][my].peso = 20;
            break;
        case MAGMA:
            matriz[mx][my].peso = 3;
            break;
        case FIREWORK:
            matriz[mx][my].peso = 0;
            matriz[mx][my].contador = 30;
            break;
        case TORCH:
            matriz[mx][my].peso = 20;
            break;
        case BOMB:
            matriz[mx][my].peso = 20;
            break;
        case FUSE:
            matriz[mx][my].peso = 20;
            break;
        case OIL:
            matriz[mx][my].peso = 1;
    }
}